﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace TakeoutMangesDAL
{
    public class ShopcarDAL
    {
        public static int InsertCar(Model.Shopcar sp) 
        {
            string sqlstr = "insert into Shopcar values(@ProductID,@UserID,@Isdelete)";
            SqlParameter[] pams = new SqlParameter[]
            {
                new SqlParameter("@ProductID",sp.ProductID),
                new SqlParameter("@UserID",sp.UserID),
                new SqlParameter("@Isdelete",sp.Isdelete),
            };
            return DBHelper.ExecuteNonQuery(sqlstr,pams);
        }

        public static List<Model.Product> GetShopcar(int id)
        {
            string sqlstr = "select * from Shopcar a,Product b where a.ProductID=b.ProductID and a.Isdelete<>1 and UserID=@id";
            SqlParameter[] pams = new SqlParameter[]
            {
                new SqlParameter("@id", id)
            };
            SqlDataReader reader = DBHelper.ExecuteReader(sqlstr, CommandType.Text, pams);
            List<Model.Product> list = new List<Model.Product>();
            int n = reader.FieldCount;
            while (reader.Read())
            {
                Model.Product p = new Model.Product()
                {
                    ProductID = Convert.ToInt32(reader["ProductID"]),
                    ProductName = reader["ProductName"].ToString(),
                    ProductImage = reader["ProductImage"].ToString(),
                    ProductPrice = reader["ProductPrice"].ToString(),
                    ProductSales = reader["ProductSales"].ToString(),
                    CategoryID = Convert.ToInt32(reader["CategoryID"]),
                    EnterpriseID = Convert.ToInt32(reader["EnterpriseID"]),
                    Isdelete = Convert.ToInt32(reader["Isdelete"]),
                };
                list.Add(p);
            }
            if (list.Count > 0)
                return list;
            return null;
        }

        public static int deletecarshow(Shopcar s) 
        {
            string sqlstr = "delete from Shopcar where ProductID=@ProductID and UserID=@UserID";
            SqlParameter[] pams = new SqlParameter[]
            {
                new SqlParameter("@ProductID",s.ProductID),
                new SqlParameter("@UserID",s.UserID),
            };
            return DBHelper.ExecuteNonQuery(sqlstr,pams);
        }
    }
}
